package com.axend.lib_base.view.dialog

import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.view.WindowManager
import androidx.databinding.DataBindingUtil
import com.axend.lib_base.R
import com.axend.lib_base.databinding.BaseDialogCustomTipsBinding
import com.axend.lib_base.ext.dp2px

/**
 * 自定义dialog
 */
class CustomTipsDialog(
    context: Context,
    title: String,
    content: String,
    onOkListener: DialogInterface.OnClickListener,
) : Dialog(context, R.style.Common_CustomDialog) {

    private var dialogBinding: BaseDialogCustomTipsBinding


    companion object{
        @Volatile
        private var instance: CustomTipsDialog? = null

        fun getInstance(context: Context,title: String,content: String,onOkListener: DialogInterface.OnClickListener):CustomTipsDialog{
            return instance ?: synchronized(this){
                instance ?: createInstance(context, title,content,onOkListener)
                    .also { instance = it }
            }
        }

        private fun createInstance(context: Context,title: String,content: String,onOkListener: DialogInterface.OnClickListener):CustomTipsDialog{
            return CustomTipsDialog(context,title,content,onOkListener)
        }
    }

    init {
        dialogBinding =
            DataBindingUtil.inflate(layoutInflater, R.layout.base_dialog_custom_tips, null, false)
        setContentView(dialogBinding.root)
        setCanceledOnTouchOutside(false)
        val window = window
        window!!.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
        //设置提示框位置
        val lp = window!!.attributes
        //lp.gravity = Gravity.TOP | Gravity.LEFT;
        //lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL;
        //lp.x = DisplayUtils.dip2px(53);
        //lp.y = DisplayUtils.dip2px(186);
        lp.width = dp2px(280f)
        lp.height= dp2px(150f)
        window.attributes = lp
        dialogBinding.tvTips.text = content
        dialogBinding.tvTipsTitle.text = title
        dialogBinding.btOk.setOnClickListener {
            onOkListener?.onClick(this, 0)
        }
    }
}